package com.base.controller;

import com.base.po.voip.*;
import com.base.service.*;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 方案文章
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_CompanyArticle {
	@Autowired
	private CompanyArticleService companyArticleService;
	@Autowired
	private UsersService usersService;

	// 方案文章首页
	@RequestMapping("/companyArticleIndex")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		Page page = new Page();
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<CompanyArticle> companyArticle = null;

		page.setCurrentPageNo(1);
		page.setRecordTotal(companyArticleService.countByParentID(adminUserId, null, null));
		companyArticle = companyArticleService.selectByParentID(page, adminUserId, null,null);

		request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		request.setAttribute("companyArticle", companyArticle);
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_CompanyArticle");
		return modelAndView;
	}
	
	@RequestMapping("/selectCompanyArticle")
	public ModelAndView selectCompanyArticle(HttpServletRequest request, Integer curPage) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");

		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

        List<CompanyArticle> companyArticle = null;

        page.setCurrentPageNo(curPage);
        page.setRecordTotal(companyArticleService.countByParentID(adminUserId, null, null));
        companyArticle = companyArticleService.selectByParentID(page, adminUserId, null, null);

        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("companyArticle", companyArticle);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_CompanyArticle");
		return modelAndView;
	}
}
